﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ATest.Bll;
using System.Data.Linq;

namespace ATest.Tests.ModelTests
{
    [TestClass]
    public class TestTest
    {
        private static Test CreatePopulatedTest()
        {
            return new Test()
            {
                Name = "Test Name"
            };
        }

        [TestMethod]
        public void InitaliseTest()
        {
            var test = new ATest.Bll.Test();
            Assert.AreEqual(0, test.Id);
            Assert.AreEqual(DateTime.MinValue, test.Created);
            Assert.AreEqual(null, test.CreatedBy);
            Assert.AreEqual(DateTime.MinValue, test.LastUpdated);
            Assert.AreEqual(null, test.LastUpdatedBy);
            Assert.AreEqual(null, test.Name);
            Assert.AreEqual(DateTime.MinValue, test.Created);
            Assert.AreEqual(DateTime.MinValue, test.LastUpdated);
        }


        [TestMethod]
        public void Name_SetToNull_Causes_RuleViolation()
        {
            var test = CreatePopulatedTest();
            test.Name = null;
            Assert.AreEqual(1, test.GetRuleViolations().Count());
            Assert.AreEqual("Name", test.GetRuleViolations().First().PropertyName);
            Assert.AreEqual("Name must be set", test.GetRuleViolations().First().ErrorMessage);
        }

        [TestMethod]
        public void Validate_OnInsert_Sets_CreatedDate()
        {
            DateTime start = DateTime.Now;
            var test = CreatePopulatedTest();
            test.PerformValidation(ChangeAction.Insert);

            Assert.AreNotEqual(DateTime.MinValue, test.Created);
            Assert.IsTrue(start <= test.Created);
            Assert.IsTrue(DateTime.Now >= test.Created);
        }

        [TestMethod]
        public void Validate_OnInsert_Sets_CreatedBy()
        {
            var test = CreatePopulatedTest();
            test.PerformValidation(ChangeAction.Insert);

            Assert.IsNotNull(test.CreatedBy);
            Assert.AreEqual(System.Threading.Thread.CurrentPrincipal.Identity.Name, test.CreatedBy);
        }

        [TestMethod]
        public void ValidateOnInsert_Sets_LastUpdated()
        {
            DateTime start = DateTime.Now;

            var test = CreatePopulatedTest();
            test.PerformValidation(ChangeAction.Insert);

            Assert.AreNotEqual(DateTime.MinValue, test.LastUpdated);
            Assert.IsTrue(start <= test.LastUpdated);
            Assert.IsTrue(DateTime.Now >= test.LastUpdated);
        }

        [TestMethod]
        public void Validate_OnUpdate_Sets_CreatedBy()
        {
            var test = CreatePopulatedTest();
            test.PerformValidation(ChangeAction.Update);

            Assert.IsNotNull(test.LastUpdatedBy);
            Assert.AreEqual(System.Threading.Thread.CurrentPrincipal.Identity.Name, test.LastUpdatedBy);
        }

        [TestMethod]
        public void ValidateOnUpdate_Sets_LastUpdated()
        {

            DateTime start = DateTime.Now;

            var test = CreatePopulatedTest();
            test.PerformValidation(ChangeAction.Update);


            Assert.AreNotEqual(DateTime.MinValue, test.LastUpdated);
            Assert.IsTrue(start <= test.LastUpdated);
            Assert.IsTrue(DateTime.Now >= test.LastUpdated);
        }

    }
}
